<template>
  <div class="w-full flex items-center">
    <el-date-picker
      v-model="startTime"
      :type="dateType"
      :placeholder="props.placeholder"
      clearable
      class="mb-[0px]"
      v-if="dateType != 'datetime'"
      :value-format="valueFormat"
    />
    <el-date-picker
      v-model="startTime"
      type="datetime"
      :placeholder="props.placeholder"
      clearable
      :value-format="valueFormat"
      class="mb-[0px]"
      v-else
    />
    <span style="width: 20px; text-align: center; font-size: 14px">至</span>
    <el-date-picker
      v-model="endTime"
      :type="dateType"
      :placeholder="props.placeholder"
      clearable
      :value-format="valueFormat"
      class="mb-[0px]"
      v-if="dateType != 'datetime'"
    />
    <el-date-picker
      v-model="endTime"
      type="datetime"
      :placeholder="props.placeholder"
      clearable
      :value-format="valueFormat"
      class="mb-[0px]"
      v-else
    />
    <el-select v-model="dateType" style="flex-basis: 145px; flex-shrink: 0" @change="typeChange">
      <el-option
        :label="item.label"
        :value="item.type"
        v-for="item in selectOptions"
        :key="item.type"
      />
    </el-select>
  </div>
</template>

<script setup>
const startTime = defineModel("startTime");
const endTime = defineModel("endTime");
const dateType = ref("datetime");
// const valueFormat = ref("YYYY-MM-DD HH:mm:ss");
const valueFormat = computed(() => {
  if (dateType.value == "datetime") {
    return "YYYY-MM-DD HH:mm:ss";
  } else if (dateType.value == "date") {
    return "YYYY-MM-DD";
  } else if (dateType.value == "month") {
    return "YYYY-MM";
  } else {
    return "YYYY";
  }
});
const props = defineProps({
  placeholder: {
    type: String,
    default: "",
  },
});
const selectOptions = [
  {
    label: "年月日时分秒",
    type: "datetime",
  },
  {
    label: "年月日",
    type: "date",
  },
  {
    label: "年月",
    type: "month",
  },
  {
    label: "年度",
    type: "year",
  },
];
//选择的时间类型改变
const typeChange = () => {
  startTime.value = "";
  endTime.value = "";
};
</script>

<style scoped lang="scss"></style>
